<template>
  <div>
    <div class="postage">
      <Card dis-hover title="邮费相关设置">
        <Form :model="setting" :label-width="120" label-position="left">
          <FormItem label="请勾选启用的策略">
            <Checkbox v-model="setting.province" @on-change="subPostage">根据省份定邮费</Checkbox>
            <Checkbox v-model="setting.order" @on-change="subPostage">根据订单总价定邮费</Checkbox>
            <Checkbox v-model="setting.profitPostage" @on-change="subPostage">根据利润定邮费</Checkbox>
            <Checkbox v-model="setting.free" @on-change="subPostage">免邮设置</Checkbox>
          </FormItem>
          <FormItem label="请勾选支持送货方式">
            <Checkbox v-model="setting.door" @on-change="sendDoor">送货上门</Checkbox>
            <Checkbox v-model="setting.home" @on-change="sendAddress">上门提货</Checkbox>
            <Checkbox v-model="setting.express" @on-change="subDelivery">快递</Checkbox>
            <Checkbox v-model="setting.logistics" @on-change="subDelivery">物流</Checkbox>
          </FormItem>
          <FormItem label="免邮设置">
            订单总价<InputNumber :min="0" v-model="setting.orderPrice"/>
            利润百分比 <InputNumber :min="0" :max="100" v-model="setting.profit"/>
            <Button type="primary" @click="freeSet">确定</Button>
          </FormItem>
          <FormItem label="请选择邮费计算方法">
            <RadioGroup v-model="setting.count" @on-change="radioChange">
              <Radio label="1">平均邮费</Radio>
              <Radio label="2">最低邮费</Radio>
              <Radio label="3">最高邮费</Radio>
            </RadioGroup>
          </FormItem>
          <FormItem label="省份邮费加权值">
            <InputNumber v-model="setting.sortWeight" :editable="false" :min="1" :max="10" placeholder="1~10" @on-change="weightInputBlur"/>
            <Poptip placement="right">
              <Icon type="help-circled" size="25"></Icon>
              <div slot="content">
                <div>
                  <p>设置权重可以使邮费计算更加合理，特别是偏远地区的邮费，默认情况下，除了省份邮费策略外其他策略的权重都为1。</p>
                  <p>例如，一笔订单的区间邮费策略计算结果是10，利润邮费策略的计算结果是15，省份邮费是20，省份邮费权重为3，</p>
                  <p>那么在平均计算方式下，最后的邮费计算结果为 : (10 + 15 + 20 * 3) / 5 = 17</p>
                </div>
              </div>
            </Poptip>
          </FormItem>
        </Form>
      </Card>
    </div>
    <Modal
      v-model="showDoor"
      title="送货上门范围"
      @on-ok="doorOk"
      @on-cancel="doorCancel">
      <Input v-model="doorRange" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="送货上门地址（5~1000字）"></Input>
      送货上门最小金额：<InputNumber v-model="setting.doorToDoorMinAmount" :min="0"/>
    </Modal>
    <Modal
      v-model="showAddress"
      title="上门自提准确地址"
      @on-ok="addressOk"
      @on-cancel="addressCancel">
      <Table :columns="addressColumn" :data="addressList" @on-selection-change="selectChange"></Table>
    </Modal>
  </div>
</template>

<script>
export default {
  name: 'postage-set',
  data () {
    return {
      url: {
        set: '/branch/setting/list',
        freeUpdate: '/branch/free/postage/setting/update',
        postageUpdate: '/branch/postage/setting/update',
        mode: '/branch/postage/calculate/mode/update',
        weight: '/branch/province/postage/weight/update',
        delivery: 'branch/delivery/setting/update',
        addressList: '/shipping/address/list'
      },
      setting: {
        province: false,
        order: false,
        free: false,
        profitPostage: false,
        count: '',
        sortWeight: null,
        orderPrice: null,
        profit: null,
        door: false,
        express: false,
        home: false,
        logistics: false,
        doorToDoorMinAmount: 0
      },
      // 送货上门
      doorRange: '',
      showDoor: false,
      // 上门提货地址
      addressList: [],
      showAddress: false,
      selectAddressId: [],
      addressColumn: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '发货人',
          key: 'send',
          render: (h, params) => {
            return h('div', [
              h('p', params.row.name),
              h('p', params.row.phone)
            ])
          }
        },
        {
          title: '地址',
          key: 'address',
          render: (h, params) => {
            return h('div', [
              h('p', params.row.province + params.row.city + params.row.county),
              h('p', params.row.address)
            ])
          }
        }
      ]
    }
  },
  methods: {
    freeSet () {
      let params = {
        payment: this.setting.orderPrice * 100,
        profit: this.setting.profit
      }
      this.$ajax.post(this.url.freeUpdate, JSON.stringify(params)).then(res => {
        if (res.data.success) {
          this.$Message.success('更新成功')
        }
      })
    },
    subPostage () {
      let params = {
        useFreePostageSetting: this.setting.free,
        usePaymentIntervalPostageSetting: this.setting.order,
        useProfitPostageSetting: this.setting.profitPostage,
        useProvincePostageSetting: this.setting.province
      }
      this.$ajax.post(this.url.postageUpdate, JSON.stringify(params)).then(res => {
        if (res.data.success) {
          this.$Message.success('更新成功')
        }
      })
    },
    subDelivery () {
      let params = {
        supportDoorToDoor: this.setting.door,
        supportExpressDelivery: this.setting.express,
        supportHomeDelivery: this.setting.home,
        supportLogisticsDelivery: this.setting.logistics
      }
      this.$ajax.post(this.url.delivery, JSON.stringify(params)).then(res => {
        if (res.data.success) {
          this.$Message.success('更新成功')
        }
      })
    },
    radioChange (val) {
      this.$ajax.post(this.url.mode, JSON.stringify(val)).then(res => {
        if (res.data.success) {
          this.$Message.success('更新成功')
        }
      })
    },
    weightInputBlur () {
      this.$ajax.post(this.url.weight, JSON.stringify(this.setting.sortWeight)).then(res => {
        if (res.data.success) {
          this.$Message.success('更新成功')
        }
      })
    },
    sendDoor (val) {
      if (val) {
        this.showDoor = true
      } else {
        this.subDelivery()
      }
    },
    doorOk () {
      let params = {
        supportDoorToDoor: this.setting.door,
        supportExpressDelivery: this.setting.express,
        supportHomeDelivery: this.setting.home,
        supportLogisticsDelivery: this.setting.logistics,
        doorToDoorRange: this.doorRange,
        doorToDoorMinAmount: this.setting.doorToDoorMinAmount * 100
      }
      this.$ajax.post(this.url.delivery, JSON.stringify(params)).then(res => {
        if (res.data.success) {
          this.$Message.success('更新成功')
        }
      })
    },
    doorCancel () {
      this.setting.door = false
      this.showDoor = false
    },
    // 上门提货方法
    sendAddress (val) {
      if (val) {
        this.showAddress = true
      } else {
        this.subDelivery()
      }
      if (!this.addressList[0]) {
        this.$ajax.post(this.url.addressList).then(res => {
          if (res.data.success) {
            this.addressList = res.data.data
          }
        })
      }
    },
    addressOk () {
      let params = {
        supportDoorToDoor: this.setting.door,
        supportExpressDelivery: this.setting.express,
        supportHomeDelivery: this.setting.home,
        supportLogisticsDelivery: this.setting.logistics,
        shippingAddressId: this.selectAddressId[0]
      }
      this.$ajax.post(this.url.delivery, JSON.stringify(params)).then(res => {
        if (res.data.success) {
          this.$Message.success('更新成功')
        }
      })
    },
    addressCancel () {
      this.setting.home = false
      this.showAddress = false
    },
    selectChange (select) {
      select.forEach(item => {
        this.selectAddressId.push(item.id)
      })
    },
    initSetting () {
      this.$ajax.post(this.url.set).then(res => {
        this.setting.count = res.data.data.calculateMode + ''
        this.setting.orderPrice = res.data.data.freePostageMinPrice / 100
        this.setting.profit = res.data.data.freePostageMinProfit
        this.setting.sortWeight = res.data.data.provincePostageWeight
        this.setting.province = res.data.data.isUseProvincePostageSetting === 1
        this.setting.order = res.data.data.isUsePaymentIntervalPostageSetting === 1
        this.setting.profitPostage = res.data.data.isUseProfitPostageSetting === 1
        this.setting.free = res.data.data.isUseFreePostageSetting === 1
        this.setting.door = res.data.data.isSupportDoorToDoor === 1
        this.setting.express = res.data.data.isSupportExpressDelivery === 1
        this.setting.home = res.data.data.isSupportHomeDelivery === 1
        this.setting.logistics = res.data.data.isSupportLogisticsDelivery === 1
        this.setting.doorToDoorMinAmount = res.data.data.doorToDoorMinAmount / 100
        this.doorRange = res.data.data.doorToDoorRange
      })
    }
  },
  created () {
    this.initSetting()
  }
}
</script>

<style scoped>

</style>
